package com.klhappy.houyuan.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.activity.Add_AddressActivity;
import com.klhappy.houyuan.bean.AddressBean.AddressBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.utils.ConfigManager;
import com.klhappy.houyuan.utils.NotifyMethod;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class AddAddressAdapter extends BaseAdapter {

    private List<AddressBean.Records> lists;
    private Context mContext;
    private Map<Integer, Boolean> isSelect;
    private ConfigManager manager;

    public AddAddressAdapter(Context context, List<AddressBean.Records> lists) {
        this.lists = lists;
        this.mContext = context;

        manager = new ConfigManager(context);
        String id = manager.get(KeyConstant.ADDRESS, "");
        isSelect = new HashMap<>();
        if (id.equals("")){
            for (int i = 0; i < lists.size(); i++) {
                if (i == 0){
                    isSelect.put(i, true);
                }else {
                    isSelect.put(i, false);
                }
            }
        }else {
            for (int k = 0; k < lists.size(); k++){
                if (id.equals("" + lists.get(k).getId())){
                    for (int i = 0; i < lists.size(); i++) {
                        if (i == k){
                            isSelect.put(i, true);
                        }else {
                            isSelect.put(i, false);
                        }
                    }
                }
            }
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.receving_address_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String name = lists.get(position).getUser_name();
        String phone = lists.get(position).getOwn_mob_no();
        String address = lists.get(position).getAddress();
        if (!TextUtils.isEmpty(name)) {
            holder.ivRecever.setText(name);
        }
        if (!TextUtils.isEmpty(phone)) {
            holder.tvPhone.setText(phone);
        }
        if (!TextUtils.isEmpty(address)) {
            holder.tvAddress.setText(address);
        }

//        holder.cbDefault.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked){
//                    NotifyMethod.getInstance().postNotification(Add_AddressActivity.AVG ,lists.get(position));
//                }
//                //先把链表中的数据取代掉
//                isSelect.put(position, isChecked);
//                if (buttonView.isChecked()) {
//                    for (int i = 0; i < lists.size(); i++) {
//                        //把其他的checkbox设置为false
//                        if (i != position) {
//                            isSelect.put(i, false);
//                        }
//                    }
//                }
//                //通知适配器更改
//                notifyDataSetChanged();
//
//            }
//        });
        holder.cbDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                if (cb.isChecked()){
                    isSelect.put(position, true);
                    for (int i = 0; i < lists.size(); i++) {
                        //把其他的checkbox设置为false
                        if (i != position) {
                            isSelect.put(i, false);
                        }
                    }
                    NotifyMethod.getInstance().postNotification(Add_AddressActivity.AVG ,lists.get(position));
                    notifyDataSetChanged();
                }
            }
        });
        holder.cbDefault.setChecked(isSelect.get(position));
        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.tv_recever)
        TextView ivRecever;
        @InjectView(R.id.tv_phone)
        TextView tvPhone;
        @InjectView(R.id.tv_address)
        TextView tvAddress;
        @InjectView(R.id.cb_default)
        CheckBox cbDefault;

        public boolean flag;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

}
